﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using KlavoNotifications.Fetch;
using KlavoNotifications.Storage;
using KlavoNotifications.Utils;
using KlavoNotifications.ViewModels.Notification;
using KlavoNotifications.ViewModels.Tray;

namespace KlavoNotifications.Notification
{
    public class Notifier
    {
        private readonly NotificationStorage _storage;
        private readonly TaskBar _taskbar;
        private readonly List<int> _reportedGames;


        public Notifier()
        {
            _storage = App.Get<NotificationStorage>();
            _taskbar = App.Get<TaskBar>();
            _reportedGames = new List<int>();
        }

        public void Start()
        {
            Fetcher fetcher = App.Get<Fetcher>();
            fetcher.FetchDataComleted += FetchDataComleted;
        }

        void FetchDataComleted(FetchData data)
        {
            List<Game> result = new List<Game>();
            foreach (Game game in data.Games)
            {
                //check by game name
                if (_storage.IsVocabularies)
                {
                    string vocName;
                    if (game.Voc != null)
                    {
                        vocName = game.Voc.Name;
                    }
                    else
                    {
                        vocName = ConvertUtils.ConvertGameTypeToString(game.GameType);
                    }
                    if (_storage.Vocabularies.Any(r => r.ToLower() == vocName.ToLower()))
                    {
                        result.Add(game);
                    }
                }

                //check by player name
                if (_storage.IsPlayers)
                {
                    if (game.Players.Any(r=>_storage.Players.Any(z=>z.ToLower() == r.Name.ToLower())))
                        result.Add(game);
                }

                //check by regular competition
                if (_storage.IsCompetitions && game.IsRegularCompetition)
                {
                    switch (game.RegularCompetitionX)
                    {
                        case 2:
                            if (_storage.IsX2)
                                result.Add(game);
                            break;
                        case 3:
                            if (_storage.IsX3)
                                result.Add(game);
                            break;
                        case 4:
                            if (_storage.IsX4)
                                result.Add(game);
                            break;
                        case 5:
                            if (_storage.IsX5)
                                result.Add(game);
                            break;
                    }                    
                }
            }

            result = result.Distinct().ToList();

            foreach (Game game in result.ToList())
            {
                if (_reportedGames.Contains(game.Id))
                    result.Remove(game);
                else
                    _reportedGames.Add(game.Id);
            }

            ShowBalloon(result, data.Timestamp);
        }

        private void ShowBalloon(List<Game> data, int timestamp)
        {
            if (data.Any())
            {
                BalloonViewModel viewModel = new BalloonViewModel(data, timestamp);
                _taskbar.ShowBalloon(viewModel);
            }
        }
    }
}